package com.example.huadi1.service.Imp;

import com.example.huadi1.dto.*;
import com.example.huadi1.mapper.*;

import com.example.huadi1.service.TextbookService;
import com.example.huadi1.utils.AjaxResult;
import com.example.huadi1.utils.PageUtils;
import com.example.huadi1.utils.Request.LoseRequest;
import com.example.huadi1.utils.Request.SinglePageRequest;
import com.example.huadi1.utils.Response.ClassGetBook;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//@Slf4j
@Service
public class TextbookServiceImp implements TextbookService {

   private static final Logger logger = LoggerFactory.getLogger(TextbookServiceImp.class);
    @Autowired
    private TextbookMapper textbookMapper;
    @Autowired
    private DemandMapper demandMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseSelectionMapper courseSelectionMapper;
    @Autowired
    private TextbookChangeMapper textbookChangeMapper;
    @Autowired
    private StockChangeMapper stockChangeMapper;
    public AjaxResult queryTextbook(SinglePageRequest singlePageRequest){
        PageHelper.startPage(singlePageRequest.getPageNum(), singlePageRequest.getPageSize());
        List<Textbook> textbooks=textbookMapper.selectAllTextbook();
        System.out.println(textbooks);
        PageInfo<Textbook> pageAll = PageInfo.of(textbooks);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
    public AjaxResult addTextbook(Textbook textbook){
        textbookMapper.addTextbook(textbook);
        textbookChangeMapper.insertTextbookChange(new TextbookChange(textbook.getTextbookId(),textbook.getCourseName(),textbook.getDepartmentId(),textbook.getPressId(),textbook.getPrice(),"增加",textbook.getTextbookName()));
        Integer num=0;
        List<Course> temp=courseMapper.findByName(textbook.getCourseName());
        for (int i = 0; i < temp.size(); i++) {
            num+= courseSelectionMapper.getStudentNum(temp.get(i).getCourseId()) ;
            num+= courseMapper.getTeacherNum(temp.get(i).getCourseId()) ;
            // 处理obj
        }
        Demand demand=new Demand(textbook.getTextbookId(),num,textbook.getDepartmentId(),0,0);
        demandMapper.insertDemand(demand);
        return AjaxResult.success();
    }
    public AjaxResult deleteTextbook(Textbook textbook){
        textbookMapper.deleteTextbook(textbook);
        textbookChangeMapper.insertTextbookChange(new TextbookChange(textbook.getTextbookId(),textbook.getCourseName(),textbook.getDepartmentId(),textbook.getPressId(),textbook.getPrice(),"删除",textbook.getTextbookName()));
        demandMapper.deleteDemand(textbook.getTextbookId());
        return AjaxResult.success();
    }
    public AjaxResult updateTextbook(Textbook textbook){
        Textbook textbook1 = textbookMapper.findById(textbook.getTextbookId());
        if(textbook.getStock()==null) textbook.setStock(textbook1.getStock());
        textbookMapper.updateTextbook(textbook);
        if(textbook1.getStock()!=textbook.getStock()) stockChangeMapper.insertStockChange(new StockChange(textbook.getTextbookId(),textbook.getStock(),textbook.getCourseName(),textbook.getDepartmentId(),textbook.getPressId(),textbook.getPrice(),textbook.getTextbookName(),textbook.getStock()-textbook1.getStock()));
        else textbookChangeMapper.insertTextbookChange(new TextbookChange(textbook.getTextbookId(),textbook.getCourseName(),textbook.getDepartmentId(),textbook.getPressId(),textbook.getPrice(),"更新",textbook.getTextbookName()));
        if(!textbook1.getCourseName().equals(textbook.getCourseName())){
            demandMapper.deleteDemand(textbook.getTextbookId());
            Integer num=0;
            List<Course> temp=courseMapper.findByName(textbook.getCourseName());
            for (int i = 0; i < temp.size(); i++) {
                num+= courseSelectionMapper.getStudentNum(temp.get(i).getCourseId()) ;
                num+=courseMapper.getTeacherNum(temp.get(i).getCourseId()) ;
                // 处理obj
            }
            Demand demand=new Demand(textbook.getTextbookId(),num,textbook.getDepartmentId(),0,0);
            demandMapper.insertDemand(demand);
        }
        return AjaxResult.success();
    }
    public AjaxResult findById(Textbook textbook){
        Textbook textbook1 = textbookMapper.findById(textbook.getTextbookId());
        return AjaxResult.success(textbook1);
    }

    public AjaxResult lose(LoseRequest loseRequest){
        Textbook textbook = textbookMapper.findById(loseRequest.getTextbookId());
        textbook.setStock(Math.max(0,textbook.getStock()-loseRequest.getLoseNum()));
        updateTextbook(textbook);
        return AjaxResult.success();
    }
}
